#include "../include/structure.h"

// 初始化socket
int initSocket(int *socket_fd, char *ip, char *port){
    *socket_fd = socket(AF_INET, SOCK_STREAM, 0);
	
	int reuse = 1;
	setsockopt(*socket_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof reuse);


	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(atoi(port));
	addr.sin_addr.s_addr = inet_addr(ip);

	int ret_bind = bind(*socket_fd, (struct sockaddr*)&addr, sizeof addr);
	ERROR_CHECK(ret_bind, -1, "bind");
	
	int ret_listen = listen(*socket_fd, 10);
	ERROR_CHECK(ret_listen, -1, "listen");

	return 0;
}